Repository navigation
Improve performance of Utils.ArrayEquals - #1331
Merged
Merged
Conversation
Carti-it
commented
Sep 6, 2026
| return false; | ||
| return ArrayEqual(a, 0, b, 0, Math.Max(a.Length, b.Length)); | ||
|
|
||
| return ArrayEqual(a, 0, b, 0, a.Length); |
Contributor
Author
There was a problem hiding this comment.
Originally, I was interested in
+#if HAS_SPAN
+ return a.AsSpan().SequenceEqual(b);
+#else
return ArrayEqual(a, 0, b, 0, a.Length);
+#endifbut it behaves like this
| Method | a | b | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| ArrayEqual_Master | Byte[32] | Byte[32] | 22.2745 ns | 0.4059 ns | 0.4832 ns | 24.86 | 0.64 | - | NA |
| ArrayEqual_PR | Byte[32] | Byte[32] | 3.6044 ns | 0.0907 ns | 0.0848 ns | 4.02 | 0.11 | - | NA |
| ArrayEqual_Master | Byte[32] | Byte[32] | 1.3504 ns | 0.0594 ns | 0.0851 ns | 1.51 | 0.10 | - | NA |
| ArrayEqual_PR | Byte[32] | Byte[32] | 2.9751 ns | 0.0841 ns | 0.0826 ns | 3.32 | 0.10 | - | NA |
So for byte arrays that are equal it goes from 22 ns -> ~4 ns but it's worse for non-equal byte arrays. I guess non-equality is in general more common. One would have to go into "amortized time complexity"... well, I guess I cannot win with it.
Carti-it
force-pushed
the
bench-array-equals
branch
2 times, most recently
from
September 6, 2026 20:30
ee468f2 to
9bc1860
Compare
Carti-it
force-pushed
the
bench-array-equals
branch
from
September 6, 2026 21:38
9bc1860 to
87bd177
Compare
| return ArrayEqual(a, 0, b, 0, Math.Max(a.Length, b.Length)); | ||
|
|
||
| if (a.Length != b.Length) | ||
| return a.Length == 0 && b.Length == 0; |
Collaborator
There was a problem hiding this comment.
why not
if (a.Length != b.Length)
return false;
Contributor
Author
There was a problem hiding this comment.
It should be like that. I fixed it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.